package com.lfy.ai;

import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 策略工厂类，用于获取不同AI类型的策略
 * @author lvfeiyang
 */
@Component
public class AiStrategyFactory implements InitializingBean, ApplicationContextAware {
    private ApplicationContext applicationContext;
    private List<AiChatStandard> list;


    @Override
    public void afterPropertiesSet() throws Exception {
        list = new ArrayList<>();
        Map<String, AiChatStandard> beansOfType =
                applicationContext.getBeansOfType(AiChatStandard.class);
        list.addAll(beansOfType.values());
    }

    @Override
    public void setApplicationContext(@NotNull ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    public AiChatStandard getStrategy(Integer type) {
        if(type == null){
            return null;
        }
        if(CollectionUtils.isEmpty(list)){
            return null;
        }
        for(AiChatStandard strategy : list){
            if(strategy.choice(type)){
                return strategy;
            }
        }

        return null;
    }
}
